﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ELS.Template.DragContent
{
    public partial class RadioItem : UserControl, DragInterface
    {
        public bool inTemplate = false;
        public String mContent = "";
        public bool mAllowComment = false;
        public bool isEditing = false;

        public RadioItem()
        {
            InitializeComponent();
        }

        public UserControl makeNewObject()
        {
            RadioItem item = new RadioItem();
            item.inTemplate = true;
            return item;
        }

        public bool isInTemplate()
        {
            return inTemplate;
        }

        private void RadioItem_MouseDown(object sender, MouseEventArgs e)
        {
            DragDropEffects effect = DoDragDrop(this, DragDropEffects.Copy);
        }

        public void disableControls()
        {
        }

        public void enableControls()
        {
        }

        private void RadioItem_MouseMove(object sender, MouseEventArgs e)
        {
           
        }

        public void setContent(String content, bool allowComment)
        {
            if ((content == null) || (content.Equals("")))
            {
                contentText.Text = Utils.Utils.DEFAULT_ANSWER_TEXT;
                contentText.ForeColor = Color.Red;
                mAllowComment = false;
            }
            else
            {
                mContent = content;
                mAllowComment = allowComment;
                contentText.Text = mContent;
                contentText.ForeColor = Color.Black;
                //if (allowComment)
                //{
                //    contentText.Text += "\n__________________________________";
                //}
            }
        }

        private void RadioItem_Load(object sender, EventArgs e)
        {
            if ((contentText.Text == null) || (contentText.Text.Equals("")))
            {
                contentText.Text = Utils.Utils.DEFAULT_ANSWER_TEXT;
            }
        }

        private void contentText_MouseEnter(object sender, EventArgs e)
        {
            this.BackColor = Color.LightGray;
        }

        private void contentText_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = Color.White;
        }

        private void contentText_MouseDown(object sender, MouseEventArgs e)
        {
            if (isEditing) return;

            isEditing = true;
            int diff = this.Height - contentText.Height;

            contentText.Hide();
            if (contentText.Text != null)
            {
                if (!contentText.Text.Equals("") && !contentText.Text.Equals(Utils.Utils.DEFAULT_ANSWER_TEXT))
                {
                    editControl.contextBox.Text = contentText.Text;
                }
            }

            int calcHeight = contentText.Height + 10;
            if (calcHeight < 100)
                calcHeight = 100;

            editControl.Size = new Size(contentText.Width, calcHeight);
            editControl.contextBox.Height = editControl.Height - 30;

            //CanhBX add wordwrap & scrollbar START
            editControl.contextBox.WordWrap = true;
            editControl.contextBox.ScrollBars = ScrollBars.Vertical;
            //CanhBX add wordwrap & scrollbar END

            this.Height = editControl.Height + diff;

            editControl.Show();
            editControl.contextBox.Focus();
        }

        private void editControl_Leave(object sender, EventArgs e)
        {
            if (!isEditing) return;

            int diff = this.Height - editControl.Height;
            editControl.Hide();
            // CanhBX update content text START
            contentText.MaximumSize = new Size(contentText.Width, 18);
            contentText.MinimumSize = new Size(contentText.Width, 18);
            contentText.AutoEllipsis = true;
            // CanhBX update content text END
            setContent(editControl.contextBox.Text, editControl.allowCommentBox.Checked);

            this.Height = contentText.Height + diff;
            contentText.Show();
            isEditing = false;
        }
    }
}
